#include "Shader.h"
#include <SEDebug/Log/Log.h>
#include <glad/glad.h>

namespace SEngine
{
    Shader::Shader()
        // : m_name(name)
    { 
        m_id = glCreateProgram();
    }

    Shader::~Shader()
    { 
        glDeleteProgram(m_id); 
    }

    void Shader::Use()
    { 
        glCall(glUseProgram(m_id)); 
    }

    int Shader::FindLocationIndex(const std::string & name)
    {
        int index = glGetUniformLocation(m_id, name.c_str());
        if (index == -1) {
            Log::GetInstance().Warn("Can not find Uniform: {}", name);
        }
        return index;
    }

    void Shader::SetUniform_(int idx, int t) { glUniform1i(idx, t); }
    void Shader::SetUniform_(int idx, float t) { glUniform1f(idx, t); }
    void Shader::SetUniform_(int idx, glm::vec2 t) { glUniform2f(idx, t.x, t.y); }
    void Shader::SetUniform_(int idx, glm::vec3 t) { glUniform3f(idx, t.x, t.y, t.z); }
    void Shader::SetUniform_(int idx, glm::vec4 t) { glUniform4f(idx, t.x, t.y, t.z, t.w); }
    void Shader::SetUniform_(int idx, glm::mat3 t) { glUniformMatrix3fv(idx, 1, GL_FALSE, glm::value_ptr(t)); }
    void Shader::SetUniform_(int idx, glm::mat4 t) { glUniformMatrix4fv(idx, 1, GL_FALSE, glm::value_ptr(t)); }
    void Shader::SetUniform_(int idx, Shader::Sampler sampler2D) { 
        if (sampler2D.texture) 
            sampler2D.texture->get().Bind(static_cast<int>(sampler2D.id));
        glUniform1i(idx, static_cast<int>(sampler2D.id));
    }

} // namespace SEngine

